package com.tust.app.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.tust.app.domain.Answer;
import com.tust.app.domain.Topic;
import com.tust.app.service.AnswerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;
import javax.servlet.http.HttpServletRequest;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
@Controller
public class AnswerController {

    @Autowired
    private AnswerService answerService;

    @RequestMapping("/apage")
    public String apage(HttpServletRequest request) {
        PageHelper.startPage(1, 3);
        List<Answer> answer = answerService.getAllAnswers();
        PageInfo page = new PageInfo(answer);
        request.setAttribute("pageInfo", page);
        return "home";
    }

    @RequestMapping("/apage/{pageNum}")
    public String apage2(@PathVariable("pageNum") Integer pageNum, HttpServletRequest request){
        PageHelper.startPage(pageNum, 3);
        List<Answer> answer = answerService.getAllAnswers();
        PageInfo page = new PageInfo(answer);
        request.setAttribute("pageInfo", page);
        return "home";
    }

    @RequestMapping("/aanswer")
    public String list(Integer pn, HttpServletRequest request) {
        if (pn == null) {
            pn = 1;
        }
        PageHelper.startPage(pn, 3);
        List<Answer> answer = answerService.getAllAnswers();
        PageInfo page = new PageInfo(answer);
        request.setAttribute("answerList", page); // Ensure attribute name matches JSP
        return "admin/answer/allAnswers";
    }

    @RequestMapping("/aanswer2")
    public String list2(Integer pn, HttpServletRequest request) {
        if (pn == null) {
            pn = 1;
        }
        PageHelper.startPage(pn, 3);
        List<Answer> answer = answerService.getAnswersByState(1); // 只获取 state2 为 1 的留言
        PageInfo page = new PageInfo(answer);
        request.setAttribute("answerList", page); // 确保属性名称与 JSP 匹配
        return "admin/answer/fallAnswers";
    }

    @RequestMapping("/writeAnswer")
    public String writeAnswer(String content,Integer state2, HttpServletRequest request) {

        // 构造一个 Answer 对象
        Answer answer = new Answer();
        answer.setContent(content);
        answer.setState2(1);

        // 设置留言用户
        String answeruser = (String) request.getSession().getAttribute("username");
        if (answeruser == null) {
            answeruser = "defaultUser"; // 默认用户，可以根据实际情况修改
        }
        answer.setAnsweruser(answeruser);

        // 设置发布时间为当前时间
        Date date = new Date();
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        String pubtime2 = format.format(date);
        answer.setPubtime2(pubtime2);

        // 设置主题 ID 和状态
        Integer topicId = Integer.parseInt(request.getParameter("topicId"));
        answer.setTopicId(topicId);
        answer.setState2(1); // 假设状态为1表示未屏蔽，可以根据实际情况修改

        // 添加留言
        answerService.addAnswer(answer);

        // 查询所有状态为1的留言，传递给页面
        PageHelper.startPage(1, 10);
        List<Answer> answers = answerService.getAnswersByState(1);
        PageInfo<Answer> pageInfo = new PageInfo<>(answers);
        request.setAttribute("answerList", pageInfo);
        return "admin/answer/fallAnswers";
    }

    @GetMapping("/updateAnswerState")
    @ResponseBody
    public ModelAndView updateAnswerState(@RequestParam("id") Integer id) {
        Answer answer = answerService.getAnswerById(id);
        if (answer != null) {
            // 切换留言状态
            if (answer.getState2() == 1) {
                answer.setState2(0);
            } else {
                answer.setState2(1);
            }
            answerService.updateAnswer(answer);
        }
        // 重定向到显示所有留言的页面
        return new ModelAndView("redirect:aanswer");
    }

    @RequestMapping("/deleteAnswer")
    public String deleteAnswer(Integer id){
        //完成删除的功能
        boolean b = answerService.deleteAnswer(id);
        if (b){
            return "forward:/aanswer";
        }
        return "error";
    }
}
